package com.imay.live.fragment.room;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;


/**
 * Created by jeaman on 2017/5/10.
 */

public class LevelUpgradeFragment2 extends BaseDialogFragment implements View.OnClickListener {
    private final static String BUNDLE_NAME_LEVEL = "level";
    private final static String BUNDLE_NAME_LIVE = "live";

    private final static int[] LEVEL_DIGITS = {R.mipmap.ico_levelup_0, R.mipmap.ico_levelup_1, R.mipmap.ico_levelup_2, R.mipmap.ico_levelup_3, R.mipmap.ico_levelup_4,
            R.mipmap.ico_levelup_5, R.mipmap.ico_levelup_6, R.mipmap.ico_levelup_7, R.mipmap.ico_levelup_8, R.mipmap.ico_levelup_9};

    private ImageView mTypeTv;
    private ImageView mDecadeTv;
    private ImageView mUnitTv;
    private TextView mMessageTv;
    private TextView mLevelOldTv;
    private TextView mLevelNewTv;
    private TextView mRewardTv;

    private Runnable mDismissRunnable;

    public static LevelUpgradeFragment2 newInstance(boolean live, int level) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(BUNDLE_NAME_LIVE, live);
        bundle.putInt(BUNDLE_NAME_LEVEL, level);
        LevelUpgradeFragment2 fragment = new LevelUpgradeFragment2();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_level_upgrade2;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTypeTv = $(R.id.level_upgrade_iv_type);
        mDecadeTv = $(R.id.level_upgrade_iv_decade);
        mUnitTv = $(R.id.level_upgrade_iv_unit);

        mMessageTv = $(R.id.level_upgrade_tv_message);
        mLevelOldTv = $(R.id.level_upgrade_tv_old_level);
        mLevelNewTv = $(R.id.level_upgrade_tv_new_level);
        mRewardTv = $(R.id.level_upgrade_tv_reward);

        $(R.id.level_upgrade_iv_close).setOnClickListener(this);

        boolean live = getArguments().getBoolean(BUNDLE_NAME_LIVE);
        int level = getArguments().getInt(BUNDLE_NAME_LEVEL);
        mMessageTv.setText(live ? R.string.level_upgrade_anchor_upgrade : R.string.level_upgrade_self_upgrade);
        mLevelOldTv.setText(getString(R.string.level_upgrade_level, level - 1));
        mLevelNewTv.setText(getString(R.string.level_upgrade_level, level));

        mTypeTv.setImageResource(live ? R.mipmap.pnl_03_room_zhubo_upgrade : R.mipmap.pnl_03_room_yonghu_upgrade);
        int decade = level / 10;    //十位
        int unit = level % 10;  //个位
        if (decade > 0) {
            mDecadeTv.setVisibility(View.VISIBLE);
            mDecadeTv.setImageResource(LEVEL_DIGITS[decade]);
        } else {
            mDecadeTv.setVisibility(View.GONE);
        }
        mUnitTv.setImageResource(LEVEL_DIGITS[unit]);
        mDismissRunnable = new Runnable() {
            @Override
            public void run() {
                if (isAdded() && !isDetached()) {
                    dismiss();
                }
            }
        };
        mTypeTv.postDelayed(mDismissRunnable, 2000);
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mTypeTv != null) {
            mTypeTv.removeCallbacks(mDismissRunnable);
        }
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.level_upgrade_iv_close:
                dismiss();
                break;
            case R.id.level_upgrade_lav_animation:  //加速关闭
                break;
        }
    }

}
